import { defineStore } from 'pinia';
import type { ArticleColumnRecord } from '/#/store';
import { queryArticleColumnList } from '/@/api/recommend';

export const useArticleColumnStore = defineStore({
  id: 'article-column',
  state: () => ({
    articleColumns: [] as ArticleColumnRecord[],
  }),
  getters: {
    getArticleColumns(): ArticleColumnRecord[] | null {
      return this.articleColumns.length ? this.articleColumns : null;
    },
  },
  actions: {
    async fetchArticleColumns(): Promise<void> {
      const res = await queryArticleColumnList();
      this.setArticleColumns([
        {
          id: 0,
          name: '全部',
          icon: '',
        },
        ...res,
      ]);
    },
    setArticleColumns(articleColumns: ArticleColumnRecord[]): void {
      this.articleColumns = articleColumns;
    },
  },
});
